package com.opo5.logisticsmanagement.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.opo5.logisticsmanagement.Vo.PageVo;
import com.opo5.logisticsmanagement.common.R;
import com.opo5.logisticsmanagement.domain.Depot;
import com.opo5.logisticsmanagement.domain.Locator;
import com.opo5.logisticsmanagement.domain.Material;
import com.opo5.logisticsmanagement.service.DepotService;
import com.opo5.logisticsmanagement.service.LocatorService;
import com.opo5.logisticsmanagement.service.MaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/materials")
@CrossOrigin
public class MaterialController {

    @Autowired
    private MaterialService materialService;

    @Autowired
    private DepotService depotService;

    @Autowired
    private LocatorService locatorService;

    /**
     * 添加物料信息
     * @param material
     * @return
     */
    @PostMapping
    public R save(@RequestBody Material material){
        log.info("添加物料信息");
        String number = materialService.produceNumber();
        material.setMaterialNumber(number);

        //设置物料的仓库id
        String depotName = material.getDepotName();
        LambdaQueryWrapper<Depot> lqw=new LambdaQueryWrapper();
        lqw.eq(Depot::getDepotName,depotName);
        Depot depot = depotService.getOne(lqw);
        material.setDepotId(depot.getId());

        //设置物料的库位id
        Locator locator = locatorService.getLocatorByDepotNameLocatorName(material.getDepotName(), material.getLocatorName());
        material.setLocatorId(locator.getId());

        materialService.save(material);
        return R.success("添加成功");
    }

    /**
     * 删除物料信息
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R delete(@PathVariable Integer id){
        log.info("删除仓库信息");
        materialService.removeById(id);
        return R.success("删除成功");
    }

    /**
     * 编辑物料信息
     * @param material
     * @return
     */
    @PutMapping
    public R update(@RequestBody Material material){
        log.info("编辑物料信息");
        materialService.updateById(material);
        return R.success("编辑成功");
    }

    /**
     * 分页条件查询
     */
    @GetMapping("/{currentPage}/{pageSize}")
    public R getPage(@PathVariable Integer currentPage,@PathVariable Integer pageSize,Material material ){
        System.out.println(material);
        log.info("分页条件查询");
        IPage<Material> page = materialService.getPage(currentPage, pageSize, material);
        return R.success(page);
    }


    /**
     * 通过id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R getAll(@PathVariable Integer id){
        Locator locator = materialService.getAll(id);
        return R.success(locator);
    }

    /**
     * 分页多表条件查询
     * @param currentPage
     * @param pageSize
     * @param material
     * @return
     */
    @GetMapping("condition/{currentPage}/{pageSize}")
    public R getPageByCondition(@PathVariable Integer currentPage,@PathVariable Integer pageSize,Material material){
        System.out.println(currentPage);
        System.out.println(pageSize);
        System.out.println(material.getDepotName());
        System.out.println(material.getLocatorName());

//        if (!material.getDepotName().isEmpty()){
//            currentPage=1;
//
//        }

        if (material.getDepotName().isEmpty()){
            material.setDepotName(null);
        }
        if (material.getLocatorName().isEmpty()){
            material.setLocatorName(null);
        }
        PageVo materialPage = materialService.getPageByCondition(currentPage, pageSize, material);
        return R.success(materialPage);
    }


}
